package com.gec.servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gec.dao.UserDao;
import com.gec.entity.User;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置一下这个编码格式 request.setCharacterEncoding("UTF-8");
		// 获取表单提交过来的数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String telephone = request.getParameter("telephone");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String address = request.getParameter("address");
		// 判断一下这个账号是否存在
		UserDao userDao = new UserDao(); 
		User user =null; 
		user = userDao.getUserByUsername(username); 
		if(user != null) { 
			request.setAttribute("msg", "");
			request.getRequestDispatcher("register.jsp").forward(request,response); 
		} else { // 账号没有被注册过的情况 
			user = new User();
		}
		user.setUid(UUID.randomUUID().toString());
		user.setUsername(username); user.setPassword(password);
		user.setName(name); user.setEmail(email);
		user.setTelephone(telephone); user.setSex(sex);
		user.setBirthday(birthday); user.setAddress(address); //
		// 调用添加数据到数据库的方法
		int result = userDao.addUser(user);
		if(result <= 0) { 
			request.setAttribute("msg", "");
		 request.getRequestDispatcher("register.jsp").forward(request,
		response); } 
		else { 
			response.sendRedirect("login.jsp");
			}
		} 
}
